#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include "testutils.h"

void tribute_test()
{
  int i, k, j, pass, tempCoin, tempAction, tempCount, numPlayers, coin_bonus;
  struct gameState state;

  for(i = 0; i < MAX_ITERATIONS; i++){
    coin_bonus = 0;
    pass = 0;

    numPlayers = 2;

    init_test_data(&state, numPlayers);

    srand(time(NULL));

    for(k = 0; k < numPlayers; k++){
      // set deck
      state.deckCount[k] = 50;
      for(j = 0; j < state.deckCount[k]; j++){
        state.deck[k][j] = random_card();
      }

      // set hand
      state.handCount[k] = 5;
      for(j = 0; j < state.handCount[k]; j++){
        state.hand[k][j] = random_card();
      }
    }
    state.hand[0][4] = tribute;

    printf("ITERATION %d\n", i);

    if(cardEffect(state.hand[0][4], -1, -1, -1, &state, 4, &coin_bonus) != 0)
      printf("Failure, tribute effect no returned\n");
    else
      pass++;

    if(state.hand[1][3] == state.hand[1][4]){
      if(state.hand[1][3] > 0 && state.hand[1][3] < 4){
	if(state.handCount[0] == 7){
	  pass++;
	  tempCount = 2;
	} else
	  printf("Failure, player did not gain 2 cards when top 2 cards are duplicate\n");
      } else if(state.hand[1][3] > 4 && state.hand[1][3] < 7){
	if(state.coins == 2){
	  pass++;
	  tempCoin = 2;
	} else
	  printf("Failure, player did not gain 2 coins when top 2 cards are duplicate\n");
      } else if(state.hand[1][3] > 6){
	if(state.numActions == 3){
	  pass++;
	  tempAction = 2;
	} else
	  printf("Failure, player did not gain 2 actions when top 2 cards are duplicate\n");
      }

      if(pass == 2)
	printf("Success, all tests passed when top 2 card are duplicate\n");

    } else {
      
      if(state.hand[1][4] > 0 && state.hand[1][4] < 4){
	if(tempCount == 2){
	  if(state.handCount[0] == 9)
	    pass++;
	  else
	    printf("Failure, player did not gain 2 more cards\n");
	} else {
	  if(state.handCount[0] == 7)
	    pass++;
	  else
	    printf("Failure, player did not gain 2 cards\n");
	}
      } else if(state.hand[1][4] > 4 && state.hand[1][4] < 7){
	if(tempCoin == 2){
	  if(state.coins == 4)
	    pass++;
	  else
	    printf("Failure, player did not gain 2 more coins\n");
	} else {
	  if(state.coins == 2)
	    pass++;
	  else
	    printf("Failure, player did not gain 2 coins\n");
	}
      } else if(state.hand[1][4] > 6){
	if(tempAction == 2){
	  if(state.numActions == 5)
	    pass++;
	  else
	    printf("Failure, player did not gain 2 more actions\n");
	} else {
	  if(state.numActions == 3)
	    pass++;
	  else
	    printf("Failure, player did not gain 2 actions\n");
	}
      }
      
      if(pass == 3)
	printf("Success, all tests passed when top 2 cards are unique\n");
    }
  }
}

int main()
{
  tribute_test();
  return 0;
}
